
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picPath: app.globalData.picPath,
    productList: [],
    boughtList: [],
    isFinish: false,
    totalNum: 0,
    totalMoney: 0,
    totalGlod: 0,
    active: 0,
    page: 1,
    typeList: [],
    type: 0,
    index: 0,
    activeIndex: -1
  },

  topage(e) {
    var product = e.currentTarget.dataset.flag;
    console.log(product);
    if (app.globalData.teacherId != 0) {
      ks.navigateTo({
        url: '/packageA/pages/buy/buy?product=' + JSON.stringify(product)
      })
    } else {
      ks.navigateTo({
        url: '/packageA/pages/buy/buy?scene=' + product.productId
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(app.globalData.teacherId);
    this.checkLogin()
    // this.getType()
  },

  checkLogin() {
    var isLogin = ks.getStorageSync("isLogin");
    console.log(isLogin);
    if (isLogin != 1) {
      console.log("进哪里了1");
      // 登录
      ks.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var url = app.globalData.url + "user/ksGetOpenId";
          app.wxRequest(url, {
            code: res.code,
          }, res => {
            console.log("实现执行这里吗");
            console.log(res);
            if (res.flag) {

              var userInfo = res.data.teacher;
              app.globalData.openId = userInfo.openId;
              app.globalData.teacherId = userInfo.teacherId;
              app.globalData.isLogin = 1;
              ks.setStorageSync('isLogin', 1);
              ks.setStorageSync('openId', userInfo.openId);
              ks.setStorageSync('teacherId', userInfo.teacherId);
              ks.setStorageSync('teacherDetail', userInfo);
              app.globalData.teacherDetail = userInfo;

              console.log(app.globalData);

              this.getType()
            }
          }, err => { })
        }
      })
    } else {
      app.globalData.openId = ks.getStorageSync("openId");
      app.globalData.teacherId = ks.getStorageSync("teacherId");
      app.globalData.teacherDetail = ks.getStorageSync("teacherDetail");
      this.getType()
    }
  },


  getType() { //获取首页数据
    console.log(app.globalData);
    ks.showLoading({ // 显示加载中loading效果 
      title: "加载中",
      mask: true //开启蒙版遮罩
    });
    var url = app.globalData.url + "management/getProductTypeAll";
    var that = this;
    app.wxRequest(url, {
      flag: 1
    }, res => {
      console.log(res);
      var datas = res.data;
      var type = datas[0].typeId;
      for (let i = 0; i < datas.length; i++) {
        datas[i].productList = [];
        datas[i].page = 1;
      }
      console.log(datas);
      if (res.flag) {
        that.setData({
          typeList: datas,
          type: type
        })
        this.getList(1);
      }
      ks.hideLoading();
      ks.stopPullDownRefresh()
    },
      err => {
        console.log(err);
      })
  },

  getList(a) {
    console.log(app.globalData);
    ks.showLoading({ // 显示加载中loading效果 
      title: "加载中",
      mask: true //开启蒙版遮罩
    });
    var url = app.globalData.url + "goldShop/getGoldShop";
    var that = this;
    app.wxRequest(url, {
      teacherId: app.globalData.teacherId,
      page: this.data.page,
      size: 4,
      productType: this.data.type
    }, res => {
      console.log(res);
      var datas = res.data;
      if (res.flag) {
        datas.products = datas.products.filter(item => {
          return item.mp4Duration != ''
        })

        if (datas.products.length < 3) {
          that.setData({
            isFinish: true
          })
        } else {
          that.setData({
            isFinish: false
          })
        }
        var productList = that.data.productList;
        datas.products.forEach(ele => {
          productList.push(ele)
        })

        console.log(productList);
        console.log(that.data);
        that.setData({
          boughtList: datas.boughtList,
          // boughtList: [],
          productList: productList
        })
        console.log(that.data.typeList);
      }

      ks.hideLoading();
      ks.stopPullDownRefresh()
    },
      err => {
        console.log(err);
      })
  },

  toAll() {
    var list = this.data.boughtList
    console.log(list);
    ks.navigateTo({
      url: '/packageA/pages/myBuy/myBuy?list=' + JSON.stringify(list),
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1,
      productList: []
    })
    this.getList();
    this.setData({
      isFinish: false
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(!this.data.isFinish);
    if (!this.data.isFinish) {
      var that = this;
      var page = that.data.page + 1
      that.setData({
        page: page
      })
      that.getList()
    } else {
      ks.showToast({
        title: '没有更多了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },



  toBack() {
    ks.navigateBack()
  },
  checkVideo(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index
    })
    console.log(index);

    this.videoContext = ks.createVideoContext('myVideo', this); // 	创建 video 上下文 VideoContext 对象。
    this.videoContext.requestFullScreen({ // 设置全屏时视频的方向，不指定则根据宽高比自动判断。
      direction: 90 // 屏幕逆时针90度
    });
  },

  // 视频播放结束
  endAction() {
    this.videoContext = ks.createVideoContext('myVideo', this);
    this.videoContext.exitFullScreen(); //退出全屏
    this.setData({
      activeIndex: -1
    })
  },

  // 视频全屏 退出全屏
  screenChange(e) {
    let fullScreen = e.detail.fullScreen //值true为进入全屏，false为退出全屏
    if (!fullScreen) { //退出全屏
      this.setData({
        activeIndex: -1
      })
    }
  },
})